package advancedFeatures;

import java.util.Arrays;
import java.util.List;

/**
 * Java泛型基础概念演示
 * 1. 泛型类
 * 2. 泛型方法
 * 3. 类型参数命名约定
 */
public class GenericBasics {
    // 泛型类示例
    public static class Container<T> {
        private T value;
        
        public Container(T value) {
            this.value = value;
        }
        
        public T getValue() {
            return value;
        }
        
        public void setValue(T value) {
            this.value = value;
        }
    }
    
    // 泛型方法示例
    public static <T> void printArray(T[] array) {
        for (T element : array) {
            System.out.print(element + " ");
        }
        System.out.println();
    }
    
    // 多个类型参数示例
    public static class Pair<K, V> {
        private K key;
        private V value;
        
        public Pair(K key, V value) {
            this.key = key;
            this.value = value;
        }
        
        // getters and setters
    }

    static class Student implements Comparable<Student> {
        private String name;
        private int score;

        public Student(String name, int score) {
            this.name = name;
            this.score = score;
        }

        @Override
        public int compareTo(Student other) {
            // 按分数升序排序
            return this.score - other.score;
        }

        @Override
        public String toString() {
            return name + " (" + score + ")";
        }
    }
    
    public static void main(String[] args) {
        // 使用泛型类
        Container<String> stringContainer = new Container<>("Hello泛型");
        Container<Integer> intContainer = new Container<>(100);
        
        // 使用泛型方法
        Integer[] numbers = {1, 2, 3, 4, 5};
        String[] words = {"Java", "泛型", "编程"};
        
        printArray(numbers);
        printArray(words);

//        List<Student> students = Arrays.asList(
//                new Student("Alice", 90),
//                new Student("Bob", 80),
//                new Student("Charlie", 85)
//        );
//        System.out.println("排序前：" + students);
//        for (Student student : students){
//            System.out.println(student);
//        }
    }
} 